package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.ProductAttribute;
import com.qijian.maindata.domain.query.ProductAttributeQuery;
import com.qijian.maindata.service.IProductAttributeService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品属性Controller
 *
 * @author ruoyi
 * @date 2021-08-18
 */
@Api(tags = "产品属性")
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/attribute")
public class ProductAttributeController extends BaseController {
    private final IProductAttributeService productAttributeService;

    /**
     * 查询产品属性列表
     */
    @PreAuthorize("@ss.hasAnyPermi('maindata:item:list,business:attribute:list,business:eqp:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductAttributeQuery query, IPage<ProductAttribute> page) {
        return getDataTable(productAttributeService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 查询全部产品属性列表
     */
    //@PreAuthorize("@ss.hasAnyPermi('maindata:item:list,business:attribute:list,business:eqp:list,business:repairTicket:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ProductAttributeQuery query) {
        List<ProductAttribute> data = productAttributeService.list(WhereEntityTool.invoke(query));
        return getDataTable(data);
    }

    /**
     * 导出产品属性列表
     */
    @PreAuthorize("@ss.hasAnyPermi('maindata:item:export,business:attribute:export')")
    @Log(title = "产品属性", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductAttributeQuery query) {
        List<ProductAttribute> list = productAttributeService.list(WhereEntityTool.invoke(query));
        ExcelUtil<ProductAttribute> util = new ExcelUtil<>(ProductAttribute.class);
        return util.exportExcel(list, "产品属性数据");
    }

    /**
     * 获取产品属性详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('maindata:item:query,business:attribute:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productAttributeService.getById(id));
    }

    /**
     * 新增产品属性
     */
    @PreAuthorize("@ss.hasAnyPermi('maindata:item:add,business:attribute:add')")
    @Log(title = "产品属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductAttribute productAttribute) {
        if (StringUtils.isBlank(productAttribute.getCode())) {
            productAttribute.setCode("ATTR-" + String.format("%06d", productAttributeService.count() + 1));
        }
        return toAjax(productAttributeService.save(productAttribute));
    }

    /**
     * 修改产品属性
     */
    @PreAuthorize("@ss.hasAnyPermi('maindata:item:edit,business:attribute:edit')")
    @Log(title = "产品属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductAttribute productAttribute) {
        return toAjax(productAttributeService.updateById(productAttribute));
    }

    /**
     * 删除产品属性
     */
    @PreAuthorize("@ss.hasAnyPermi('maindata:item:remove,business:attribute:remove')")
    @Log(title = "产品属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productAttributeService.removeByIds(ids));
    }
}
